package CellEngine;

import java.util.HashMap;

public class CellMap {
	public final int width, height, depth;
	private HashMap<Integer,obj> objects; // Could use an ArrayList or something, but as far as I'm concerned, ArrayLists automatically adjust the indexes of other object if one object is deleted, therefore making them unusable if you want a consistent identation
	private int objectIDCounter = 0;
	private Cell[][][] cMap;
	// Build a new map with the given width, height and depth
	// Warning: Huge maps (i.E. 1000*1000*10) will use a lot of memory (700*700*700 already uses about 100 MBytes)
	public CellMap (int argWidth, int argHeight, int argDepth)
	{
		
		width = argWidth;
		height = argHeight;
		depth = argDepth;
		cMap = new Cell[depth][width][height];
		for(int z=0; z < depth; z++)
			for(int x=0; x < width; x++)
				for(int y=0; y < height; y++)
					cMap[z][x][y] = new Cell(x, y, z);		
	}
	// Return the cell at the given coordinates.
	// Will throw an IllegalArgumentException if the coordinates are invalid
	public Cell getCell(int x, int y, int z)
	{
		if(!inMap(x, y, z))
			throw new IllegalArgumentException();
		return cMap[z][x][y];
	}
	// Returns true if the given coordinates are within the specified map borders, otherwise false
	public boolean inMap(int x, int y, int z)
	{
		if(x < 0 || x > (width - 1) || y < 0 || y > (height - 1) || z < 0 || z > (depth - 1))
			return false;
		return true;
	}
	// Returns the object of a given ID.
	public obj getObject(int objectID)
	{
		if(objects.containsKey(objectID))
			return (obj)objects.get(objectID);
		else
			return null;
	}
	
	// Only to be used internally, this function returns an ID for an object
	public int makeObjectID(obj object)
	{
		if(object.id > 0) // Object already has a valid id?
			return object.id;
		objectIDCounter++;
		return objectIDCounter;
	}
	
	public void removeObjectFromIDMap(int objectID)
	{
		if(getObject(objectID) != null)
			objects.remove(objectID);
	}
}
